package com.easygo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {

    //springRedisTemplate  这个是jedis和springBoot整合的工具类对象


    /*
    *创建jedisPoolConfig对象，在该对象中完成一些连接池配置
    * @ConfigurationProperties会将perfis相同的内容创建一个实体
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.jedis.pool")
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        System.out.println("默认值最大空闲数："+config.getMaxIdle());
        System.out.println("默认值最小空闲数："+config.getMinIdle());
        System.out.println("默认值最大李娜结束："+config.getMaxTotal());
        return config;
    }

    /*
    *创建jedisConnectionFactory：配置redis连接信息
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {

//        System.out.println("配置完毕最大空闲数：" + config.getMaxIdle());
//        System.out.println("配置完毕最小空闲数：" + config.getMinIdle());
//        System.out.println("配置完毕最大连接数：" + config.getMaxTotal());

        System.out.println("配置完毕最大空闲数：" + config.getMaxIdle());
        System.out.println("配置完毕最小空闲数：" + config.getMinIdle());
        System.out.println("配置完毕最大连接数：" + config.getMaxTotal());

//        JedisConnectionFactory factory = new JedisConnectionFactory();
//        factory.setPoolConfig(config);

        JedisConnectionFactory factory = new JedisConnectionFactory();
        //关联链接池的配置对象
        factory.setPoolConfig(config);
        return factory;
    }

    @Bean
    public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory connectionFactory){
        System.out.println("连接的地址是:"+connectionFactory.getHostName());
        System.out.println("连接的端口是:"+connectionFactory.getPort());

        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        //为KEY设置序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //为value设置序列化器
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
